Things beginners need to know about AWS Lambda – A Serverless Compute Service
When a user wants to build an application, they need a backend code that should run in response to event actions.
To manage the infrastructure and run backend code requires the user to scale a bunch of servers, manage operating system updates, apply security patches and then monitor all of these for performance and availability, is a difficult process.
This is where AWS Lambda comes into place. It helps the user focus on building applications without having to worry about their infrastructure.
What is AWS Lambda?
AWS Lambda is a serverless compute service that allows us to run the code without having to worry about provisioning or managing any server. We can run the application or backend service using AWS Lambda with no need of administration.
The code that is run on AWS Lambda is called a lambda function . Some of the languages it supports are Java, Python, Node.js, C #, PowerShell etc.
AWS Lambda follows FaaS (Function-as-a-Service).
With AWS Lambda, we only pay for what we use. We are charged based on the number of requests for the lambda functions and the duration , the time it takes for your code to execute. The AWS Lambda free usage tier includes 1M free requests per month and 400,000 GB-seconds of compute time per month.
How does AWS Lambda work?
- First, the user should create a function and add basic data to it that includes the programming language to be used in the function.
- Then the application code should be written on the lambda editor or upload it using a supported programming language in a zip file.
- To run the code, you need to trigger the lambda function with an external AWS service (as shown in diagram), which can invoke the lambda function.
- The lambda will be ready to trigger the function automatically when an event occurs within a few seconds. It provisions, manages and monitors the servers for you.
By using AWS Lambda, the user just has to upload the application code on Lambda, and it will run the code, even scale the infrastructure with high availability, which is why it is highly preferred.
The AWS Lambda Dashboard from the management console of AWS also provides viewing of account-level metrics that include error count and success rate (%), Throttles, Duration, Concurrent executions etc.
An example of the account-level metrics is shown below:
Where can AWS Lambda be used?
- File processing using AWS S3 (Real-time): We can use Amazon S3 to trigger AWS Lambda service to process data immediately after uploading it.
- Stream processing using AWS Lambda and Amazon Kinesis (Real-time): We can use AWS Lambda and Amazon Kinesis to process streaming data for application event tracking, transaction order processing etc.
- To extract, transform and load from DynamoDB table: We can use AWS Lambda service to perform data validation, filtering, sorting for every data change in a DynamoDB table and load the data to another data store.
- Web Applications: By combining AWS Lambda service with other AWS services, developers can build web applications that automatically scale according to the needs.
- Mobile Application's backend code security: We can build backends using AWS Lambda service and Amazon API Gateway to authenticate the API requests.
Where & how did I use AWS Lambda Trigger?
A trigger is a AWS Lambda resource that we configure to invoke the lambda function in response to lifecycle events, external requests, or on a schedule. The Lambda function can have multiple triggers.
During my training in the Data Analytics Department, I was given the task to get the RSS Feed and save it as an xml file in the provided S3 bucket. Later, to make the lambda function deployable using Serverless Framework. I had used Python as the programming language.
The next task was to use feedparser to get particular data from the saved xml file, and add a process to convert it to JSON format and further save the file in S3 bucket.
The last task was to configure Lambda function to execute it at every hour with a 30 minutes break. To do this, the following code can be added to the serverless.yml file. The event trigger can be manually enabled and disabled as well.
events: events: --schedule: rate: cron (* / 30 * * *? *) Enabled: true [/ yaml]
The following image shows a trigger event created for every 30 minutes, for a lambda function.
Conclusion
AWS Lambda is really easy to use. If the user has a business application that requires to run backend code, they might consider using a serverless compute service like AWS Lambda. To get started with AWS Lambda service, we use the Lambda console from the AWS Management console to create a function. In a few minutes, we can create a function. We can also view logs, metrics, and trace data.